/**
 * Created by vincent on 2017/9/25.
 */
moduleForum.controller("ForumPageQuery", function (GeneralResource, LanguageService, $scope, $log, $state, $timeout, LoadingService, ConstantService, DialogService, ForumResource) {
    initPage($scope);
    $scope.$on("$viewContentLoaded", function () {
        pageOnLoad();
        ConstantService.addState($state.current, true, LanguageService.language('评论管理'));
        $scope.pagination = new Pagination();
        $scope.pagination.setPageClickFn($scope.queryForum);
        $scope.queryForum($scope.pagination.currentPageNo);
        $scope.loadLearningMap();
        $scope.user = ConstantService.getItem('user');
    });

    $scope.topOptions = Enums.BooleanEnum.options;

    $scope.queryPara = {};
    $scope.queryForum = function (pageNo) {
        //LoadingService.show();
        ForumResource.queryPage({pageNo: pageNo - 1, pageSize: 10}, $scope.queryPara, function (queryResult) {
            $log.debug("地图查询结果", queryResult);
            $scope.queryResult = queryResult;
            $scope.pagination.setPage(queryResult, pageNo);
            //LoadingService.hide();
        }, function () {
            //LoadingService.hide();
        });
    };

    $scope.selectLearningMap = function () {
       // $scope.queryPara. = $scope.selectedLearningMap.id;
        $scope.queryPara = {
            targetId : $scope.selectedLearningMap.id
        }
        $scope.queryForum(1);
    }

    $scope.loadLearningMap = function () {
        var learningMapPara = {
            className: Models.LearningMap.className,
            query: {
                name: {
                    field: Models.LearningMap.query.name
                },
                code: {
                    field: Models.LearningMap.query.code
                }
            },
            orderBy: {
                name: {
                    field: Models.LearningMap.query.createDate,
                    sortType: 'DESC'
                }
            }
        };

        GeneralResource.queryPage({pageNo: 0, pageSize: 50}, learningMapPara, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.learningMaps = queryResult.data.datas;
        });
    }

    $scope.setForumValid = function (forum) {
        Alert.confirm(function () {
            //LoadingService.show();
            ForumResource.setValid(forum, function (forumResult) {
                $log.debug("设为有效结果", forumResult);
                angular.copy(forumResult, forum);
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success();
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    };

    $scope.setForumInvalid = function (forum) {
        Alert.confirm(function () {
            //LoadingService.show();
            ForumResource.setInvalid(forum, function (forumResult) {
                $log.debug("设为无效结果", forumResult);
                angular.copy(forumResult, forum);
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success();
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    };

    $scope.checkReply = function (forum) {
        $state.go("sz.forumReplyQuery", {
            forumId: forum.id
        });
    };

    $scope.refresh = function () {
        $scope.queryPara = {};
    };


    $scope.setTop = function (forum) {
        Alert.confirm(function () {
            //LoadingService.show();

            var bean = {
                id: forum.id,
                top: true
            };

            ForumResource.updateTop({}, bean, function (forumResult) {
                $log.debug("将该评论置顶结果", forumResult);
                angular.copy(forumResult, forum);
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success();
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    }

    $scope.cancelTop = function (forum) {
        Alert.confirm(function () {
            //LoadingService.show();

            var bean = {
                id: forum.id,
                top: false
            };

            ForumResource.updateTop({}, bean, function (forumResult) {
                $log.debug("评论设取消置顶结果", forumResult);
                angular.copy(forumResult, forum);
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success();
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    }
});